module.exports = function(app) {
	return new ChatRemote(app);
};

var ChatRemote = function(app) {
    this.app = app;
    this.channelService = app.get('channelService');
};

var chatRemote = ChatRemote.prototype;

chatRemote.add = function(uid, sid, name , flag, cb) {
    var channel  = this.channelService.getChannel(name, flag);
    var username = uid.split('*')[0];
    var route    = 'onAdd';
    var param    =  {
        user: username
    };
    if( !! channel )
    {
        console.log("*****" + route + param);
        channel.pushMessage(route, param);
        channel.add(uid, sid);
    }
    console.log("*****chatRemote.add ");
	cb(this.get(name, flag));
};

//根据房间号获取 所有用户名。
chatRemote.get = function(name, flag) {

    var channel = this.channelService.getChannel(name, flag);
    var users = [];
    if( !! channel )
    {
        users = channel.getMembers();
    }

    console.log( users );
    for(let i=0; i< users.length; i++)
    {
        users[i] = users[i].split('*')[0];
    }

    return users;
};

chatRemote.kick = function(uid, sid, name , cb) {

    var channel = this.channelService.getChannel(name, false);
    var username = uid.split('*')[0];

    if( !! channel ){
        channel.leave(uid, sid);
        var route = 'onLeave';
        var msgparam = {
            user: username
        }
        channel.pushMessage(route, msgparam);
    }
	cb();
};